package no.uib.prideconverter.gui;

import no.uib.prideconverter.PRIDEConverter;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import no.uib.prideconverter.filefilters.SequestParamsFileFilter;
import no.uib.prideconverter.filefilters.TxtFileFilter;
import uk.ac.ebi.pride.model.interfaces.mzdata.CvParam;
import uk.ac.ebi.pride.model.interfaces.mzdata.UserParam;

/**
 * This frame handles the selection of the data files to be converted for 
 * DTASelect projects.
 * 
 * @author Harald Barsnes
 * 
 * Created February 2009
 */
public class DataFileSelectionDTASelect extends javax.swing.JFrame {


    /** 
     * Opens a new DataFileSelectionDTASelect frame, and inserts stored information.
     * 
     * @param location where to position the frame
     */
    public DataFileSelectionDTASelect(Point location) {

        // sets the default wizard frame size
        this.setPreferredSize(new Dimension(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT));
        this.setSize(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT);
        this.setMaximumSize(new Dimension(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT));
        this.setMinimumSize(new Dimension(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT));

        initComponents();

        // insert stored information
        if (PRIDEConverter.getProperties().getDtaSelectFileName() != null) {
            dtaSelectFileJTextField.setText(PRIDEConverter.getProperties().getDtaSelectFileName());
        }

        if (PRIDEConverter.getProperties().getSpectrumFilesFolderName() != null) {
            spectrumFileFolderJTextField.setText(PRIDEConverter.getProperties().getSpectrumFilesFolderName());
        }

        sequestParamFileJTextField.setText("" + PRIDEConverter.getProperties().getSequestParamFile());

        // sets the icon of the frame
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().
                getResource("/no/uib/prideconverter/icons/prideConverter_16.GIF")));

        setTitle(PRIDEConverter.getWizardName() + " " +
                PRIDEConverter.getPrideConverterVersionNumber() + " - " + getTitle());

        if (location != null) {
            setLocation(location);
        } else {
            setLocationRelativeTo(null);
        }

        // check if the next button should be enabled or not
        mandatoryFieldsCheck();

        fileSelectionJPanel.requestFocus();
        setVisible(true);
    }

    /**
     * Checks if all mandatory information is filled in and enables or disables 
     * the Next button.
     */
    private void mandatoryFieldsCheck() {

        if (dtaSelectFileJTextField.getText().length() > 0 &&
                sequestParamFileJTextField.getText().length() > 0 &&
                spectrumFileFolderJTextField.getText().length() > 0) {
            nextJButton.setEnabled(true);
        } else {
            nextJButton.setEnabled(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nextJButton = new javax.swing.JButton();
        backJButton = new javax.swing.JButton();
        cancelJButton = new javax.swing.JButton();
        fileSelectionJPanel = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        dtaSelectFileJTextField = new javax.swing.JTextField();
        findDtaSelectFileJButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        spectrumFileFolderJTextField = new javax.swing.JTextField();
        spectumFileFolderJButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        helpJButton = new javax.swing.JButton();
        aboutJButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        sequestParamFileJTextField = new javax.swing.JTextField();
        sequestParamFileJButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("File Selection - Step 1 of 8");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        nextJButton.setText("Next  >");
        nextJButton.setEnabled(false);
        nextJButton.setPreferredSize(new java.awt.Dimension(81, 23));
        nextJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextJButtonActionPerformed(evt);
            }
        });

        backJButton.setText("< Back");
        backJButton.setPreferredSize(new java.awt.Dimension(81, 23));
        backJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backJButtonActionPerformed(evt);
            }
        });

        cancelJButton.setText("Cancel");
        cancelJButton.setPreferredSize(new java.awt.Dimension(81, 23));
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        fileSelectionJPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("File Selection"));

        jLabel8.setText("DTASelect File (txt version): ");
        jLabel8.setToolTipText("The DTASelect.txt file");

        dtaSelectFileJTextField.setEditable(false);
        dtaSelectFileJTextField.setToolTipText("The DTASelect.txt file");

        findDtaSelectFileJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/Directory.gif"))); // NOI18N
        findDtaSelectFileJButton.setToolTipText("Select File");
        findDtaSelectFileJButton.setBorderPainted(false);
        findDtaSelectFileJButton.setContentAreaFilled(false);
        findDtaSelectFileJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findDtaSelectFileJButtonActionPerformed(evt);
            }
        });

        jLabel10.setText("Spectrum File Folder: ");
        jLabel10.setToolTipText("The folder with the MS2 files");
        jLabel10.setMaximumSize(new java.awt.Dimension(138, 14));
        jLabel10.setMinimumSize(new java.awt.Dimension(138, 14));
        jLabel10.setPreferredSize(new java.awt.Dimension(138, 14));

        spectrumFileFolderJTextField.setEditable(false);
        spectrumFileFolderJTextField.setToolTipText("The folder with the MS2 files");

        spectumFileFolderJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/Directory.gif"))); // NOI18N
        spectumFileFolderJButton.setToolTipText("Select Folder");
        spectumFileFolderJButton.setBorderPainted(false);
        spectumFileFolderJButton.setContentAreaFilled(false);
        spectumFileFolderJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spectumFileFolderJButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout fileSelectionJPanelLayout = new org.jdesktop.layout.GroupLayout(fileSelectionJPanel);
        fileSelectionJPanel.setLayout(fileSelectionJPanelLayout);
        fileSelectionJPanelLayout.setHorizontalGroup(
            fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fileSelectionJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jLabel10, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLabel8, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(12, 12, 12)
                .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(dtaSelectFileJTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE)
                    .add(spectrumFileFolderJTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 405, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(findDtaSelectFileJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, Short.MAX_VALUE)
                    .add(spectumFileFolderJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, Short.MAX_VALUE))
                .addContainerGap())
        );

        fileSelectionJPanelLayout.linkSize(new java.awt.Component[] {jLabel10, jLabel8}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        fileSelectionJPanelLayout.setVerticalGroup(
            fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fileSelectionJPanelLayout.createSequentialGroup()
                .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(fileSelectionJPanelLayout.createSequentialGroup()
                        .add(16, 16, 16)
                        .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                            .add(dtaSelectFileJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(findDtaSelectFileJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(11, 11, 11)
                        .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                            .add(spectrumFileFolderJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(spectumFileFolderJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(fileSelectionJPanelLayout.createSequentialGroup()
                        .add(57, 57, 57)
                        .add(jLabel10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(fileSelectionJPanelLayout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(jLabel8)))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        fileSelectionJPanelLayout.linkSize(new java.awt.Component[] {dtaSelectFileJTextField, findDtaSelectFileJButton, spectrumFileFolderJTextField, spectumFileFolderJButton}, org.jdesktop.layout.GroupLayout.VERTICAL);

        jLabel3.setFont(jLabel3.getFont().deriveFont((jLabel3.getFont().getStyle() | java.awt.Font.ITALIC), jLabel3.getFont().getSize()-2));
        jLabel3.setText("Select the files you want to convert data from, and click on 'Next'  to continue.");

        helpJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/help.GIF"))); // NOI18N
        helpJButton.setToolTipText("Help");
        helpJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpJButtonActionPerformed(evt);
            }
        });

        aboutJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/prideConverter_16.GIF"))); // NOI18N
        aboutJButton.setToolTipText("About");
        aboutJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutJButtonActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Parameters"));

        jLabel1.setText("SEQUEST Params File:");
        jLabel1.setPreferredSize(new java.awt.Dimension(138, 14));

        sequestParamFileJTextField.setEditable(false);
        sequestParamFileJTextField.setToolTipText("The sequest.params file");
        sequestParamFileJTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                sequestParamFileJTextFieldKeyReleased(evt);
            }
        });

        sequestParamFileJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/Directory.gif"))); // NOI18N
        sequestParamFileJButton.setToolTipText("Select File");
        sequestParamFileJButton.setBorderPainted(false);
        sequestParamFileJButton.setContentAreaFilled(false);
        sequestParamFileJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sequestParamFileJButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(sequestParamFileJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 407, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sequestParamFileJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 29, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                            .add(sequestParamFileJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(sequestParamFileJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(17, 17, 17)
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(new java.awt.Component[] {sequestParamFileJButton, sequestParamFileJTextField}, org.jdesktop.layout.GroupLayout.VERTICAL);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(helpJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(aboutJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 335, Short.MAX_VALUE)
                        .add(backJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(nextJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(cancelJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 658, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel3)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, fileSelectionJPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(fileSelectionJPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 266, Short.MAX_VALUE)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(7, 7, 7)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(cancelJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(nextJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(backJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(helpJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(aboutJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the frame and the wizard.
     * 
     * @param evt
     */
    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        this.setVisible(false);
        this.dispose();
        PRIDEConverter.cancelConvertion();
    }//GEN-LAST:event_cancelJButtonActionPerformed

    /**
     * Closes the frame and opens the next frame (SpectraSelection).
     * 
     * @param evt
     */
    private void nextJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextJButtonActionPerformed

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        boolean saveOk = true;

        PRIDEConverter.getProperties().setSequestParamFile(sequestParamFileJTextField.getText());

        if (new File(PRIDEConverter.getProperties().getSpectrumFilesFolderName()).list().length == 0) {
            JOptionPane.showMessageDialog(this,
                    "The spectrum folder is empty! Verify that you have selected the correct folder.",
                    "Spectrum Folder Empty", JOptionPane.ERROR_MESSAGE);
            saveOk = false;
        }

        if (saveOk) {
            //new SpectraSelectionWithIdentifications(prideConverter, this.getLocation());
            //new SpectraSelectionNoIdentifications(prideConverter, this.getLocation());
            new ExperimentProperties(this.getLocation());

            this.setVisible(false);
            this.dispose();
        }

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_nextJButtonActionPerformed

    /**
     * @see #cancelJButtonActionPerformed(java.awt.event.ActionEvent)
     * 
     * @param evt
     */
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        cancelJButtonActionPerformed(null);
    }//GEN-LAST:event_formWindowClosing

    /**
     * Opens a Help dialog.
     * 
     * @param evt
     */
    private void helpJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpJButtonActionPerformed
        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        new HelpDialog(this, false, getClass().getResource("/no/uib/prideconverter/helpfiles/DataFileSelectionDTASelect.html"));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_helpJButtonActionPerformed

    /**
     * Opens an About PRIDE Converter dialog.
     * 
     * @param evt
     */
    private void aboutJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutJButtonActionPerformed
        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        new HelpDialog(this, false, getClass().getResource("/no/uib/prideconverter/helpfiles/AboutPRIDE_Converter.html"));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_aboutJButtonActionPerformed

    /**
     * Closes the frame and opens the previous frame (DataSourceSelection).
     * 
     * @param evt
     */
    private void backJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backJButtonActionPerformed

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        boolean saveOk = true;

        PRIDEConverter.getProperties().setSequestParamFile(sequestParamFileJTextField.getText());

        if (saveOk) {

            new DataSourceSelection(this.getLocation());
            this.setVisible(false);
            this.dispose();
        }

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_backJButtonActionPerformed

    /**
     * Opens a filechooser where the DTASelect file can be selected.
     * 
     * @param evt
     */
    private void findDtaSelectFileJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findDtaSelectFileJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        JFileChooser chooser = new JFileChooser(PRIDEConverter.getUserProperties().getCurrentSourceFileLocation());

        if (dtaSelectFileJTextField.getText().length() > 0) {
            chooser.setSelectedFile(new File(dtaSelectFileJTextField.getText()));
        }

        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setDialogTitle("Select the DTASelect File");
        chooser.setFileFilter(new TxtFileFilter());

        String path;

        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            path = (chooser.getSelectedFile().getAbsoluteFile().getPath());
            dtaSelectFileJTextField.setText(path);
            PRIDEConverter.getUserProperties().setSourceFileLocation(chooser.getSelectedFile().getPath());
            PRIDEConverter.getProperties().setDtaSelectFileName(path);

            PRIDEConverter.getProperties().setSpectrumTableModel(null);
            PRIDEConverter.getProperties().setInstrumentDetailsExtracted(false);
            PRIDEConverter.getProperties().setSelectedSourceFiles(new ArrayList());
            PRIDEConverter.getProperties().setSpectraSelectionCriteria(null);
            PRIDEConverter.getProperties().setDataFileHasBeenLoaded(false);
            PRIDEConverter.getProperties().setSampleDetailsExtracted(false);
            PRIDEConverter.getProperties().setContactInfoExtracted(false);
            PRIDEConverter.getProperties().setCurrentQuantificationSelection(new ArrayList());
            PRIDEConverter.getProperties().setSpectrumCvParams(new HashMap<String, ArrayList<CvParam>>());
            PRIDEConverter.getProperties().setSpectrumUserParams(new HashMap<String, ArrayList<UserParam>>());
        }

        mandatoryFieldsCheck();

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_findDtaSelectFileJButtonActionPerformed

    /**
     * Opens a filechooser where the MS2 files folder can be selected.
     * 
     * @param evt
     */
    private void spectumFileFolderJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_spectumFileFolderJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        JFileChooser chooser = new JFileChooser(
                PRIDEConverter.getUserProperties().getCurrentSourceFileLocation());

        if (spectrumFileFolderJTextField.getText().length() > 0) {
            chooser.setSelectedFile(new File(spectrumFileFolderJTextField.getText()));
        }

        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setDialogTitle("Select the Spectrum Files Folder");

        String path;

        int returnVal = chooser.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            path = (chooser.getSelectedFile().getAbsoluteFile().getPath());
            spectrumFileFolderJTextField.setText(path);
            PRIDEConverter.getUserProperties().setSourceFileLocation(chooser.getSelectedFile().getPath());
            PRIDEConverter.getProperties().setSpectrumFilesFolderName(path);

            PRIDEConverter.getProperties().setSpectrumTableModel(null);
            PRIDEConverter.getProperties().setInstrumentDetailsExtracted(false);
            PRIDEConverter.getProperties().setSelectedSourceFiles(new ArrayList());
            PRIDEConverter.getProperties().setSpectraSelectionCriteria(null);
            PRIDEConverter.getProperties().setDataFileHasBeenLoaded(false);
            PRIDEConverter.getProperties().setSampleDetailsExtracted(false);
            PRIDEConverter.getProperties().setContactInfoExtracted(false);
            PRIDEConverter.getProperties().setCurrentQuantificationSelection(new ArrayList());
            PRIDEConverter.getProperties().setSpectrumCvParams(new HashMap<String, ArrayList<CvParam>>());
            PRIDEConverter.getProperties().setSpectrumUserParams(new HashMap<String, ArrayList<UserParam>>());
        }

        mandatoryFieldsCheck();

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_spectumFileFolderJButtonActionPerformed

    /**
     * Enables or disables the Next button.
     * 
     * @param evt
     */
    private void sequestParamFileJTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sequestParamFileJTextFieldKeyReleased
        mandatoryFieldsCheck();
}//GEN-LAST:event_sequestParamFileJTextFieldKeyReleased

    /**
     * Opens a filechooser where the sequest.params file can be selected.
     * 
     * @param evt
     */
    private void sequestParamFileJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sequestParamFileJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        JFileChooser chooser = new JFileChooser(PRIDEConverter.getUserProperties().getCurrentSourceFileLocation());

        if (sequestParamFileJTextField.getText().length() > 0) {
            chooser.setSelectedFile(new File(sequestParamFileJTextField.getText()));
        }

        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setDialogTitle("Select the sequest.params File");
        chooser.setFileFilter(new SequestParamsFileFilter());

        String path;

        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            path = (chooser.getSelectedFile().getAbsoluteFile().getPath());
            sequestParamFileJTextField.setText(path);
            PRIDEConverter.getUserProperties().setSourceFileLocation(chooser.getSelectedFile().getPath());
            PRIDEConverter.getProperties().setSequestParamFile(path);

            PRIDEConverter.getProperties().setSpectrumTableModel(null);
            PRIDEConverter.getProperties().setInstrumentDetailsExtracted(false);
            PRIDEConverter.getProperties().setSelectedSourceFiles(new ArrayList());
            PRIDEConverter.getProperties().setSpectraSelectionCriteria(null);
            PRIDEConverter.getProperties().setDataFileHasBeenLoaded(false);
            PRIDEConverter.getProperties().setSampleDetailsExtracted(false);
            PRIDEConverter.getProperties().setContactInfoExtracted(false);
            PRIDEConverter.getProperties().setCurrentQuantificationSelection(new ArrayList());
            PRIDEConverter.getProperties().setSpectrumCvParams(new HashMap<String, ArrayList<CvParam>>());
            PRIDEConverter.getProperties().setSpectrumUserParams(new HashMap<String, ArrayList<UserParam>>());
        }

        mandatoryFieldsCheck();

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_sequestParamFileJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aboutJButton;
    private javax.swing.JButton backJButton;
    private javax.swing.JButton cancelJButton;
    private javax.swing.JTextField dtaSelectFileJTextField;
    private javax.swing.JPanel fileSelectionJPanel;
    private javax.swing.JButton findDtaSelectFileJButton;
    private javax.swing.JButton helpJButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton nextJButton;
    private javax.swing.JButton sequestParamFileJButton;
    private javax.swing.JTextField sequestParamFileJTextField;
    private javax.swing.JTextField spectrumFileFolderJTextField;
    private javax.swing.JButton spectumFileFolderJButton;
    // End of variables declaration//GEN-END:variables
}
